﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Essensoft.AspNetCore.Payment.Alipay.Domain;
using General.Core.Common;
using General.Entities.Models;
using General.Mvc.Filters;
using General.Services.Admin;
using General.Services.Role;
using General.Services.SystemConfig;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace General.Mvc.Controllers
{
    public class AdminController : Controller
    {
        private readonly IAdminService adminService;
        private readonly IRoleService roleService;
        private readonly ISystemConfigService systemConfigService;

        public AdminController(IAdminService adminService, IRoleService roleService,ISystemConfigService systemConfigService)
        {
            this.adminService = adminService;
            this.roleService = roleService;
            this.systemConfigService = systemConfigService;
        }


        /// <summary>
        /// 后台首页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public async Task<IActionResult> Index()
        {
            //获取用户信息
            var user = JsonHelper.Deserialize<tc_admin>(HttpContext.Session.GetString("Admin"));
            var model = await adminService.GetInfoById(user.id);
            return View(model);
        }


        /// <summary>
        /// 通知消息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("/Admin/page/tpl/tpl-message.html")]
        [AdminCheckFilter]
        public IActionResult Tpl_Message()
        {
            return View();
        }


        /// <summary>
        /// 本地便签
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("/Admin/page/tpl/tpl-note.html")]
        [AdminCheckFilter]
        public IActionResult Tpl_Note()
        {
            return View();
        }


        /// <summary>
        /// 锁屏
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("/Admin/page/tpl/tpl-lock-screen.html")]
        [AdminCheckFilter]
        public async Task<IActionResult> Tpl_Lock_Screen()
        {
            var user = JsonHelper.Deserialize<tc_admin>(HttpContext.Session.GetString("Admin"));
            var model = await adminService.GetInfoById(user.id);
            return View(model);
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("/Admin/page/tpl/tpl-password.html")]
        [AdminCheckFilter]
        public async Task<IActionResult> Tpl_UpdatePassword()
        {
            return View();
        }


        /// <summary>
        /// 皮肤设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("/Admin/page/tpl/tpl-theme.html")]
        [AdminCheckFilter]
        public async Task<IActionResult> Tpl_Theme()
        {
            return View();
        }

        /// <summary>
        /// 个人中心
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public async Task<IActionResult> User_Info()
        {
            var user = JsonHelper.Deserialize<tc_admin>(HttpContext.Session.GetString("Admin"));
            var model = await adminService.GetInfoById(user.id);
            ViewBag.rolelist = await roleService.GetListByIds(model.role ?? "");
            return View(model);
        }

        /// <summary>
        /// 工作台
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public async Task<IActionResult> WorkPlace()
        {
            //系统架构
            ViewBag.OSArchitecture = RuntimeInformation.OSArchitecture.ToString();
            //系统名称
            ViewBag.OSDescription = RuntimeInformation.OSDescription.ToString();
            //进程架构
            ViewBag.ProcessArchitecture = RuntimeInformation.ProcessArchitecture.ToString();
            //.netcore版本
            ViewBag.FrameworkDescription = RuntimeInformation.FrameworkDescription;
            var proc = Process.GetCurrentProcess();
            var mem = proc.WorkingSet64;
            var cpu = proc.TotalProcessorTime;
            //进程已使用物理内存(kb)
            ViewBag.UsedMem = mem / 1024.0;
            //进程已占耗CPU时间(ms)
            ViewBag.UsedCPUTime = cpu.TotalMilliseconds;
            //处理器数
            ViewBag.ProcessorCount = Environment.ProcessorCount;
            return View();
        }


        /// <summary>
        /// 控制台
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult Console()
        {
            return View();
        }

        /// <summary>
        /// 分析页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult DashBoard()
        {
            return View();
        }

        /// <summary>
        /// 系统设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public async Task<IActionResult> SystemConfig()
        {
            ViewBag.typeList = await systemConfigService.GetTypeList();
            ViewBag.configList = await systemConfigService.GetAllConfigInfo();
            return View();
        }

        /// <summary>
        /// 系统日志
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult LogRecord()
        {
            return View();
        }

        /// <summary>
        /// 定时任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult JobList()
        {
            return View();
        }


        /// <summary>
        /// 角色管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult RoleList()
        {
            return View();
        }

        /// <summary>
        /// 字典管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult Dictionary()
        {
            return View();
        }

        /// <summary>
        /// 区域管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult RegionList()
        {
            return View();
        }

        /// <summary>
        /// 管理员列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult AdminList()
        {
            return View();
        }


        /// <summary>
        /// 本地文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult MyFiles()
        {
            return View();
        }

        /// <summary>
        /// 代码生成
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult CodeGeneration()
        {
            return View();
        }

        


        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AdminCheckFilter]
        public IActionResult ExitLogin()
        {
            HttpContext.Session.Remove("Admin");
            return Redirect("/Admin/Login");
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCaptchaImage()
        {
            Tuple<string, int> captchaCode = CaptchaHelper.GetCaptchaCode();
            HttpContext.Session.SetString("CaptchaCode", captchaCode.Item2.ToString());
            byte[] bytes = CaptchaHelper.CreateCaptchaImage(captchaCode.Item1);
            
            return File(bytes, @"image/jpeg");
        }


        /// <summary>
        /// 快捷登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Galigeigei()
        {
            tc_admin tc_Admin = new tc_admin()
            {
                 createtime = DateTime.Now, id = 1, lastip = "", lasttime = DateTime.Now, password = "e10adc3949ba59abbe56e057f20f883e", role = "", status = 1, username = "admin", headimg= "/images/user.png"
            };
            HttpContext.Session.SetString("Admin", JsonHelper.Serialize(tc_Admin));
            return Redirect("/Admin/Index");
        }
    }
}